﻿$(function () {
    //加载省,市,区
    $.ajax({
        type: "GET",
        url: "RegionLoad",
        dataType: "JSON",
        success: function (response) {
            $.each(response, function (key, value) {
                $("#region").append("<option value=" + value.RegionId + ">" + value.RegionName + "</option>");
                $("#regionModify").append("<option value=" + value.RegionId + ">" + value.RegionName + "</option>");
            });
        },
        error: function (err) {
            alert(err.responseText);
        }
    });

    //创建收货地址表单验证
    $("#shippingAdressCreateForm").validate({
        rules: {
            ShipTo: {
                required: true
            },
            CellPhone: {
                required: true,
                isMobile: true
            },
            TelPhone: {
                isTel: true
            },
            region: {
                required: true,
                isIntGtZero: true
            },
            city: {
                required: true,
                isIntGtZero: true
            },
            county: {
                required: true,
                isIntGtZero: true
            },
            Address: {
                required: true
            }
        },
        submitHandler: function (form) {
            form.submit();
        }
    });

    //编辑收货地址表单验证
    $("#shippingAddressModifyForm").validate({
        rules: {
            ShipTo: {
                required: true
            },
            CellPhone: {
                required: true,
                isMobile: true
            },
            TelPhone: {
                isTel: true
            },
            regionModify: {
                required: true,
                isIntGtZero: true
            },
            cityModify: {
                required: true,
                isIntGtZero: true
            },
            countyModify: {
                required: true,
                isIntGtZero: true
            },
            Address: {
                required: true
            }
        },
        submitHandler: function (form) {
            form.submit();
        }
    });
});

//查看收货地址信息
var modifyShippingAddress = function (shippingId) {
    $.ajax({
        type: "GET",
        url: "ShippingAddressView?shippingId=" + shippingId,
        dataType: "JSON",
        success: function (response) {
            $('#RegionIdModify').val(response.RegionId);
            $('#hidShippingId').val(response.ShippingId);
            $('#txtShipTo').val(response.ShipTo);
            $('#txtCellPhone').val(response.CellPhone);
            $('#txtTelPhone').val(response.TelPhone);
            $('#txtAddress').val(response.Address);
            //$('#txtZipcode').val(response.Zipcode);
            if (response.IsDefault) {
                $('#selectDefaultModify').prop("checked", true);
            }
            else {
                $('#selectDefaultModify').removeAttr("checked");
            }
            loadRegionModify(response.RegionId);
        },
        error: function (err) {
            alert(err.responseText);
        }
    });
};

//删除收货地址
function deleteShippingAddress(hidUserId, hidShippingId) {
    //KevinShop.MessageBox.Confirm("", "您确定删除吗？", function (isConfirm) {
    //    if (isConfirm) {
    //        var data = {
    //            hidUserId: hidUserId,
    //            hidShippingId: hidShippingId
    //        };
    //        $.post("ShippingAddressDelete", data, function () {
    //            window.location.reload();
    //        });
    //    }
    //});
    var data = {
        hidUserId: hidUserId,
        hidShippingId: hidShippingId
    };
    $.post("ShippingAddressDelete", data, function () {
        window.location.reload();
    });
};

$(document).ready(function () {
    var len = $('.section').length;
    $('.addressNum').text(len);
    var fancyOption = {
        maxWidth: 1000,
        maxHeight: 600,
        fitToView: true,
        width: 700,
        height: 450,
        wrapCSS: 'custom-fancy-wrapper',
        helpers: {
            overlay: {
                css: {
                    'background': 'rgba(255,255,255,.6)'
                },
                closeClick: false
            }
        },
        tpl: {
            closeBtn: '<i class="fa fa-times"></i>'
        },
        autoSize: false
    };

    $('.modify').fancybox(fancyOption);
    $('.addNewAddress').fancybox(fancyOption);
    $('#equalize').equalHeights();

    $('.delThisAddress').on('click', function () {
        var userid = $(this).data('userid');
        var shippingid = $(this).data('shippingid');
        $(this).fastConfirm({
            position: "left",
            questionText: "您确定要删除这个地址吗?",
            proceedText: "确定删除",
            cancelText: "暂不删除",
            onProceed: function (trigger) {
                deleteShippingAddress(userid, shippingid);
            },
            onCancel: function (trigger) {
                $(trigger).fastConfirm('close');
            }
        });
    });
});